# -*- coding: utf-8 -*-
"""
@Time ： 2022-10-11 15:06
@Auth ： Mr.Z
@Company ：test
@Function ：测试用例汇总及错误用例信息详情统计
"""

# coding:utf8
from Common.Logger import logger
from Common.Excel import excel


class Result_res:


    def get_groups(self, result_path):
        reader = excel.get_reader(result_path)
        cases = []
        # 遍历所有sheet页面
        for n in reader.get_sheets():
            # 从第一个页面开始遍历
            reader.set_sheet(n)
            # 获取sheet的行数，用来遍历
            row = reader.rows
            # 遍历sheet里面所有用例
            lines = reader.readline()
            case = []
            for i in range(1, row):
                line = lines[i]
                # 如果是模块，证明已经读取完上一个模块的用例
                if len(line[0]) > 1:
                    # case添加到cases,重置case，用于存储下一个用例
                    if case:
                        cases.append(case)
                        case = []
                # 如果是用例名，证明已经读取完模块的一个用例，需要继续读取模块的下一个用例
                elif len(line[1]) > 1:
                    # case添加到cases,重置case，用于存储下一个用例
                    if case:
                        cases.append(case)
                        case = []
                    case.append(line)
                # 如果是步骤，line添加到case
                else:
                    case.append(line)
            # 保存最后一个模块的用例
            if case:
                cases.append(case)
        return cases

    def result(self,result_path,result_test):
        r = self.get_groups(result_path)
        # 统计错误用例数
        a = 0
        lines = []
        for i in range(0, len(r)):
            if str(r[i]).__contains__("fail"):
                lines.append(r[i])
                a = a + 1
        # 用例通过率
        res = (len(r) - a) / len(r) * 100
        # 用例总数
        sum = len(r)
        logger.info("用例总数：" + str(sum))
        ca = sum - a
        logger.info("用例通过数：" + str(ca))
        pa = "%4.2f" % res + "%"
        logger.info("用例通过率：%4.2f" % res + "%")

        # 新命名，作为保存文件
        copy_result_test = result_test.replace(".xls", "_result.xls")
        # 错误用例信息汇总
        writer = excel.get_writer(result_test, copy_result_test)
        # 切换sheet页面
        sheetname = writer.get_sheets()
        writer.set_sheet(sheetname[0])
        writer.write(0, 1, sum)
        writer.write(1, 1, ca)
        writer.write(2, 1, a)
        writer.write(3, 1, pa)
        aa = len(lines)
        # 切换sheet页面
        writer.set_sheet(sheetname[1])
        # 记录写入的行数
        num = 1
        # 遍历三维列表
        for i in range(aa):
            line = lines[i]
            # 遍历二维列表
            for j in range(len(line)):
                # 用例名
                writer.write(num + j, 0, line[j][1],4)
                # 步骤
                writer.write(num + j, 1, line[j][2],0)
                # 关键字
                writer.write(num + j, 2, line[j][3],0)
                # 参数
                writer.write(num + j, 3, line[j][4],0)
                writer.write(num + j, 4, line[j][5],0)
                writer.write(num + j, 5, line[j][6],0)
                # 状态
                if line[j][7] =="fail":
                    writer.write(num + j, 6, line[j][7],2)
                else:
                    writer.write(num + j, 6, line[j][7],3)
                # 实际结果
                if line[j][8].__contains__("Traceback"):
                    writer.write(num + j, 7, line[j][8],2)
                else:
                    writer.write(num + j, 7, line[j][8], 3)
            # 记录每个用例的占用的行数
            case_len = len(line)
            # 遍历完一个二维列表(一个用例)，记录对应的行数
            num = num + case_len
        writer.save_close()


if __name__ == '__main__':
    res = Result_res()
    res.result('../lib/inter/result_bixi.xls',"../lib/inter/test.xls")


    # res = Result_res()
    # r = res.get_groups('../lib/inter/result_bixi.xls')
    # print(r)
    # a = 0
    # lines = []
    # for i in range(0, len(r)):
    #     print(r[i])
    #     if str(r[i]).__contains__("fail"):
    #         # print(r[i])
    #         lines.append(r[i])
    #         a = a + 1
    #         # print(a)
    #
    # # 用例通过率
    # res = (len(r) - a) / len(r) * 100
    # # print(res)
    # sum =len(r)
    # print("用例总数："+str(sum))
    # ca = sum -a
    # print("用例通过数："+str(ca))
    # pa = "%4.2f" % res + "%"
    # print("用例通过率：%4.2f" % res + "%")
    # print("错误用例信息汇总：")
    # writer = excel.get_writer("../lib/inter/test.xls",'../lib/inter/test_009.xls')
    # sheetname = writer.get_sheets()
    # writer.set_sheet(sheetname[0])
    # writer.write(0, 1, sum)
    # writer.write(1, 1, ca)
    # writer.write(2, 1, a)
    # writer.write(3, 1, pa)
    # aa=len(lines)
    #
    # writer.set_sheet(sheetname[1])
    # # 记录写入的行数
    # num = 1
    #
    # # 遍历三维列表
    # for i in range(aa):
    #     line =lines[i]
    #     # print(line)
    #
    #     # 遍历二维列表
    #     for j in range(len(line)):
    #         print(line[j])
    #         # 用例名
    #         writer.write(num + j, 0, line[j][1])
    #         # 步骤
    #         writer.write(num + j, 1, line[j][2])
    #         # 关键字
    #         writer.write(num + j, 2, line[j][3])
    #         # 参数
    #         writer.write(num + j, 3, line[j][4])
    #         writer.write(num + j, 4, line[j][5])
    #         writer.write(num + j, 5, line[j][6])
    #         # 状态
    #         writer.write(num + j, 6, line[j][7])
    #         # 实际结果
    #         writer.write(num + j, 7, line[j][8])
    #
    #     # 记录每个用例的行数
    #     case_len = len(line)
    #     # 遍历完一个二维列表，记录对应的行数
    #     num = num + case_len
    #
    # writer.save_close()

